package kr.saeildamil.androidstudy_second23;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimCustom extends Activity {
	ImageView mAnimTarget;
	Button mBtnAnim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch23_animcustom);
		
		mAnimTarget=(ImageView) findViewById(R.id.ch23_animcustom_target);
	}
	public void mOnClick(View v){
		switch (v.getId()) {
		case R.id.ch23_animcustom_skew:
			mAnimTarget.startAnimation(new SkewAnim());
			break;

		case R.id.ch23_animcustom_camera:
			mAnimTarget.startAnimation(new CameraAnim());
			break;
		}
	}
	class SkewAnim extends Animation{
		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			setDuration(1000);
			setInterpolator(new LinearInterpolator());
		}
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			Matrix matrix=t.getMatrix();
			matrix.setSkew(0.5f*interpolatedTime, 0);
		}
	}
	class CameraAnim extends Animation{
		float cx,cy;
		@Override
		public void initialize(int width, int height, int parentWidth,
				int parentHeight) {
			super.initialize(width, height, parentWidth, parentHeight);
			cx=width/2;
			cy=height/2;
			setDuration(1000);
			setInterpolator(new LinearInterpolator());
		}
		@Override
		protected void applyTransformation(float interpolatedTime,
				Transformation t) {
			super.applyTransformation(interpolatedTime, t);
			Camera cam=new Camera();
			cam.rotateY(360*interpolatedTime);
			Matrix matrix=t.getMatrix();
			cam.getMatrix(matrix);
			matrix.preTranslate(-cx, -cy);
			matrix.postTranslate(cx, cy);
		}
	}
	

}
